/*==============================================================================
REPLICATION FILE
===============================================================================
Paper: "The Microfoundations of Negotiated Peace: Generalized Trust and
       Support for Peace Processes in Colombia and Guatemala"
Journal: Public Opinion Quarterly
Authors: Ryan E. Carlin, Gregory J. Love, Jennifer L. McCoy, Jelena Subotic

Data Sources:
1. NSF Survey Data (trust games and experimental measures) - 2015
2. Guatemala LAPOP Data (1999 - supplementary analysis)
3. Colombia LAPOP Data (2006-2020 - main longitudinal analysis)
==============================================================================*/

clear all
set more off
set scheme s2mono

/*------------------------------------------------------------------------------
SET UP PATHS - UPDATE THIS TO YOUR LOCAL PATH
------------------------------------------------------------------------------*/

// Set the path to where you extracted the replication files
global replication_path "."

global data_path "${replication_path}/data"
global output_path "${replication_path}/output"

// Ensure output directories exist
capture mkdir "${output_path}/logs"
capture mkdir "${output_path}/tables"
capture mkdir "${output_path}/figures"

// Create log file
capture log close
log using "${output_path}/logs/POQ_Master_Analysis.log", replace

/*==============================================================================
SECTION 1: NSF SURVEY ANALYSIS (MAIN EXPERIMENTAL RESULTS) - 2015 DATA
==============================================================================*/

display ""
display "================================================================================"
display "SECTION 1: NSF 2015 SURVEY ANALYSIS - TRUST GAMES AND PEACE ATTITUDES"
display "================================================================================"

use "${data_path}/nsf_survey_replication.dta", clear

// Set survey weights
svyset [pweight=Peso]

/*------------------------------------------------------------------------------
Variable Labels
------------------------------------------------------------------------------*/

label var IT1M "Generalized trust"
label var JB4 "Generalized trust"
label var L1 "Ideology"
label var votesantos "Vote Santos"
label var votezulaga "Vote Zuluaga"
label var sexo "Female"
label var children "# of Children"
label var education "Years of Education"
label var income "Income"
label var zona_d2 "Region: Orinoquia"
label var zona_d3 "Region: Atlantica"
label var zona_d4 "Region: Central"
label var zona_d5 "Region: Oriental"
label var zona_d6 "Region: Pacifica"
label var vic1_r "Conflict Victim"
label var conflict_zone "Conflict Region"
label var vicfarc "FARC Victim"
label var vicstate "State Victim"
label var vigilante "Vigilante Preferences"
label var pca_negrecip "Negative Reciprocity"
label var pca_recip "Reciprocity"
label var recip "Reciprocity"
label var EDAD_R "Age"
label var COLPROPAZ1 "Peace Process"
label var TJ "TJ"
label var DDR "DDR"
label var peace_scale "Peace Scale"

/*==============================================================================
SECTION 1A: DESCRIPTIVE STATISTICS TABLE FOR 2015 NSF SURVEY
==============================================================================*/

display ""
display "Creating Descriptive Statistics Table for 2015 NSF Survey"

// Generate descriptive statistics for key variables
estpost summarize IT1M JB4 COLPROPAZ1 TJ DDR peace_scale ///
    sexo EDAD_d2 EDAD_d3 EDAD_d4 EDAD_d5 children education income ///
    zona_d2 zona_d3 zona_d4 zona_d5 zona_d6 vic1_r conflict_zone

esttab using "${output_path}/tables/Descriptive_Stats_2015_NSF.csv", ///
    replace ///
    cells("mean(fmt(3)) sd(fmt(3)) min(fmt(0)) max(fmt(0)) count(fmt(0))") ///
    nomtitle nonumber ///
    title("Descriptive Statistics: 2015 NSF Survey") ///
    label ///
    csv

/*------------------------------------------------------------------------------
NSF TABLE 1: Trust and Support for Negotiated Peace (Survey Question-Based)
------------------------------------------------------------------------------*/

display ""
display "Creating Table 1: Trust and Support for Negotiated Peace (Survey Question-Based)"

// Clear any stored estimates
eststo clear

// Run regressions for IT1M (survey trust measure)
eststo it1m_m1: reg COLPROPAZ1 IT1M L1 votesantos votezulaga sexo EDAD_d2 EDAD_d3 EDAD_d4 EDAD_d5 children education income zona_d2 zona_d3 zona_d4 zona_d5 zona_d6 vic1_r conflict_zone
eststo it1m_m2: reg TJ IT1M L1 votesantos votezulaga sexo EDAD_d2 EDAD_d3 EDAD_d4 EDAD_d5 children education income zona_d2 zona_d3 zona_d4 zona_d5 zona_d6 vic1_r conflict_zone
eststo it1m_m3: reg DDR IT1M L1 votesantos votezulaga sexo EDAD_d2 EDAD_d3 EDAD_d4 EDAD_d5 children education income zona_d2 zona_d3 zona_d4 zona_d5 zona_d6 vic1_r conflict_zone
eststo it1m_m4: reg peace_scale IT1M L1 votesantos votezulaga sexo EDAD_d2 EDAD_d3 EDAD_d4 EDAD_d5 children education income zona_d2 zona_d3 zona_d4 zona_d5 zona_d6 vic1_r conflict_zone

// Export Table 1 with p-values
esttab it1m_m1 it1m_m2 it1m_m3 it1m_m4 using "${output_path}/tables/Table1_Survey_Trust_Peace.csv", ///
    replace ///
    cells(b(fmt(3) star) p(fmt(3) par)) ///
    star(* 0.10 ** 0.05 *** 0.01) ///
    stats(N r2, fmt(0 3) labels("Observations" "R-squared")) ///
    mtitles("Peace Process" "TJ" "DDR" "Peace Scale") ///
    title("Table 1: Trust and Support for Negotiated Peace (Survey Question-Based)") ///
    label ///
    csv

/*------------------------------------------------------------------------------
NSF TABLE 2: Trust and Support for Negotiated Peace (Behavioral-Based Measure)
------------------------------------------------------------------------------*/

display ""
display "Creating Table 2: Trust and Support for Negotiated Peace (Behavioral-Based Measure)"

// Clear estimates and run regressions for JB4 (behavioral trust measure)
eststo clear

eststo jb4_m1: reg COLPROPAZ1 JB4 L1 votesantos votezulaga sexo EDAD_d2 EDAD_d3 EDAD_d4 EDAD_d5 children education income zona_d2 zona_d3 zona_d4 zona_d5 zona_d6 vic1_r conflict_zone
eststo jb4_m2: reg TJ JB4 L1 votesantos votezulaga sexo EDAD_d2 EDAD_d3 EDAD_d4 EDAD_d5 children education income zona_d2 zona_d3 zona_d4 zona_d5 zona_d6 vic1_r conflict_zone
eststo jb4_m3: reg DDR JB4 L1 votesantos votezulaga sexo EDAD_d2 EDAD_d3 EDAD_d4 EDAD_d5 children education income zona_d2 zona_d3 zona_d4 zona_d5 zona_d6 vic1_r conflict_zone
eststo jb4_m4: reg peace_scale JB4 L1 votesantos votezulaga sexo EDAD_d2 EDAD_d3 EDAD_d4 EDAD_d5 children education income zona_d2 zona_d3 zona_d4 zona_d5 zona_d6 vic1_r conflict_zone

// Export Table 2 with p-values
esttab jb4_m1 jb4_m2 jb4_m3 jb4_m4 using "${output_path}/tables/Table2_Behavioral_Trust_Peace.csv", ///
    replace ///
    cells(b(fmt(3) star) p(fmt(3) par)) ///
    star(* 0.10 ** 0.05 *** 0.01) ///
    stats(N r2, fmt(0 3) labels("Observations" "R-squared")) ///
    mtitles("Peace Process" "TJ" "DDR" "Peace Scale") ///
    title("Table 2: Trust and Support for Negotiated Peace (Behavioral-Based Measure)") ///
    label ///
    csv

/*------------------------------------------------------------------------------
NSF FIGURE 1: Trust Effects Coefficient Plot
------------------------------------------------------------------------------*/

display ""
display "Creating Figure 1: Trust Effects on Peace Attitudes"

// Create coefficient plot for trust effects
eststo clear

eststo m1: reg COLPROPAZ1 JB4 L1 votesantos votezulaga sexo EDAD_d2 EDAD_d3 EDAD_d4 EDAD_d5 children education income zona_d2 zona_d3 zona_d4 zona_d5 zona_d6 vic1_r conflict_zone
eststo m2: reg TJ JB4 L1 votesantos votezulaga sexo EDAD_d2 EDAD_d3 EDAD_d4 EDAD_d5 children education income zona_d2 zona_d3 zona_d4 zona_d5 zona_d6 vic1_r conflict_zone
eststo m3: reg DDR JB4 L1 votesantos votezulaga sexo EDAD_d2 EDAD_d3 EDAD_d4 EDAD_d5 children education income zona_d2 zona_d3 zona_d4 zona_d5 zona_d6 vic1_r conflict_zone
eststo m4: reg peace_scale JB4 L1 votesantos votezulaga sexo EDAD_d2 EDAD_d3 EDAD_d4 EDAD_d5 children education income zona_d2 zona_d3 zona_d4 zona_d5 zona_d6 vic1_r conflict_zone

coefplot m1 m2 m3 m4, ///
    keep(JB4) ///
    xline(0) ///
    title("Effect of Behavioral Trust on Peace Attitudes") ///
    legend(order(2 "Peace Process" 4 "TJ" 6 "DDR" 8 "Peace Scale"))

graph export "${output_path}/figures/Figure1_Trust_Effects.pdf", replace
graph export "${output_path}/figures/Figure1_Trust_Effects.png", replace

/*------------------------------------------------------------------------------
NSF FIGURE 2: Victimhood Effects on Trust
------------------------------------------------------------------------------*/

display ""
display "Creating Figure 2: Victimhood Effects on Trust"

// Define victimhood measures for analysis
local vic_measures "vic1_r famvic1_r vic_any vicfarc_any vicstate_any"
local controls_base "sexo EDAD_d2 EDAD_d3 EDAD_d4 EDAD_d5 children education income zona_d2 zona_d3 zona_d4 zona_d5 zona_d6 conflict_zone"

// Clear estimates and run victimhood analysis
estimates clear

local j = 1
foreach vic of local vic_measures {
    eststo IT1M_vic_`j': reg IT1M `vic' `controls_base', robust
    eststo JB4_vic_`j': reg JB4 `vic' `controls_base', robust
    local j = `j' + 1
}

// Export victimhood results
esttab IT1M_vic_1 IT1M_vic_2 IT1M_vic_3 IT1M_vic_4 IT1M_vic_5 ///
    using "${output_path}/tables/Figure2_Victimhood_IT1M.csv", ///
    replace ///
    cells(b(fmt(3) star) p(fmt(3) par)) ///
    star(* 0.10 ** 0.05 *** 0.01) ///
    stats(N r2, fmt(0 3) labels("Observations" "R-squared")) ///
    mtitles("Direct" "Family" "Any" "FARC" "State") ///
    title("Figure 2: Victimhood Effects on Survey Trust (IT1M)") ///
    label ///
    csv

esttab JB4_vic_1 JB4_vic_2 JB4_vic_3 JB4_vic_4 JB4_vic_5 ///
    using "${output_path}/tables/Figure2_Victimhood_JB4.csv", ///
    replace ///
    cells(b(fmt(3) star) p(fmt(3) par)) ///
    star(* 0.10 ** 0.05 *** 0.01) ///
    stats(N r2, fmt(0 3) labels("Observations" "R-squared")) ///
    mtitles("Direct" "Family" "Any" "FARC" "State") ///
    title("Figure 2: Victimhood Effects on Trust Game (JB4)") ///
    label ///
    csv

/*==============================================================================
SECTION 2: GUATEMALA ANALYSIS (SUPPLEMENTARY)
==============================================================================*/

display ""
display "================================================================================"
display "SECTION 2: GUATEMALA ANALYSIS - SUPPLEMENTARY EVIDENCE"
display "================================================================================"

/*------------------------------------------------------------------------------
Guatemala 1999 Analysis
------------------------------------------------------------------------------*/

display "Analyzing Guatemala 1999 data..."

use "${data_path}/guatemala_1999_replication.dta", clear

// Clear estimates for Guatemala analysis
eststo clear

// Peace support regression
eststo gtm99_m1: reg peace_support trust b2.p62 p32c edad female p125 p115b p110 i.region [pweight=wtfact], beta

// Export Guatemala 1999 results
esttab gtm99_m1 using "${output_path}/tables/Table_Guatemala1999.csv", ///
    replace ///
    cells(b(fmt(3) star) p(fmt(3) par)) ///
    star(* 0.10 ** 0.05 *** 0.01) ///
    stats(N r2, fmt(0 3) labels("Observations" "R-squared")) ///
    mtitles("OLS") ///
    title("Guatemala 1999 - Trust and Peace Support") ///
    label ///
    csv

/*==============================================================================
SECTION 3: COLOMBIA LAPOP ANALYSIS (MAIN LONGITUDINAL EVIDENCE)
==============================================================================*/

display ""
display "================================================================================"
display "SECTION 3: COLOMBIA LAPOP ANALYSIS - LONGITUDINAL EVIDENCE (2006-2020)"
display "================================================================================"

use "${data_path}/colombia_lapop_replication.dta", clear

/*------------------------------------------------------------------------------
LAPOP TABLE: Over-time Relationship Between Generalized Trust and Support for Peace
------------------------------------------------------------------------------*/

display ""
display "Creating Table: Over-time Relationship Between Generalized Trust and Support for Peace"

eststo clear

// Model 1: Trust in FARC
eststo lapop_m1: mixed colb60_r q1 q2 q12 ur wealth_quin c.it1##year || year: it1

// Model 2: Support for Peace Process
eststo lapop_m2: mixed colpropaz1r q1 q2 q12 ur wealth_quin c.it1##year || year: it1

// Model 2a: Support for Peace Process with institutional trust control
eststo lapop_m2a: mixed colpropaz1r q1 q2 q12 ur wealth_quin inst_trust c.it1##year || year: it1

// Model 3: Presidential Approval
eststo lapop_m3: mixed m1_r q1 q2 q12 ur wealth_quin c.it1##year || year: it1

// Model 4: Reconciliation Possible
eststo lapop_m4: mixed colpaz6a_r q1 q2 q12 ur wealth_quin inst_trust c.it1##year || year: it1

// Export LAPOP mixed effects results
esttab lapop_m1 lapop_m2 lapop_m3 lapop_m4 using "${output_path}/tables/Table_LAPOP_Mixed_Effects.csv", ///
    replace ///
    cells(b(fmt(3) star) p(fmt(3) par)) ///
    star(* 0.10 ** 0.05 *** 0.01) ///
    stats(N ll, fmt(0 1) labels("Observations" "Log-likelihood")) ///
    mtitles("Trust FARC" "Peace Process" "Presidential" "Reconciliation") ///
    title("Over-time Relationship Between Generalized Trust and Support for Peace") ///
    label ///
    csv

/*------------------------------------------------------------------------------
LAPOP Placebo Tests
------------------------------------------------------------------------------*/

display "Running LAPOP placebo tests..."

eststo lapop_p1: mixed sd6new2 q1 q2 q12 ur wealth_quin c.it1##year || year: it1

eststo lapop_p2: mixed b47a q1 q2 q12 ur wealth_quin inst_trust c.it1##year || year: it1

// Export placebo tests
esttab lapop_p1 lapop_p2 using "${output_path}/tables/Table_LAPOP_Placebo_Tests.csv", ///
    replace ///
    cells(b(fmt(3) star) p(fmt(3) par)) ///
    star(* 0.10 ** 0.05 *** 0.01) ///
    stats(N ll, fmt(0 1) labels("Observations" "Log-likelihood")) ///
    mtitles("Local Services" "Trust Elections") ///
    title("LAPOP Placebo Tests") ///
    label ///
    csv

/*==============================================================================
SUMMARY
==============================================================================*/

display ""
display "================================================================================"
display "ANALYSIS COMPLETE"
display "================================================================================"

log close

display ""
display "Master analysis complete!"
display ""
display "Output files created in: ${output_path}"
display ""
display "TABLES:"
display "  - Descriptive_Stats_2015_NSF.csv"
display "  - Table1_Survey_Trust_Peace.csv"
display "  - Table2_Behavioral_Trust_Peace.csv"
display "  - Figure2_Victimhood_IT1M.csv"
display "  - Figure2_Victimhood_JB4.csv"
display "  - Table_Guatemala1999.csv"
display "  - Table_LAPOP_Mixed_Effects.csv"
display "  - Table_LAPOP_Placebo_Tests.csv"
display ""
display "FIGURES:"
display "  - Figure1_Trust_Effects.pdf/png"
